
/**
* Copyright(C) 老纳慢走
*/
$(document).ready(function() {
// ajax_post_json("apis/user/address", {
// "user_id" : u_id,
// "option" : "RETRIEVE_ALL",
// "access_token":access_token
// }, m_beforeSend, address_list_success, m_error, m_complete);
	getArea('#pre-city', $("#pre-province").val(), 2);

    $("#pre-province").change(function(){
    	var pid = $("#pre-province").val();
    	getArea("#pre-city", pid, 2);
    });

    $("#pre-city").change(function(){
    	var pid = $("#pre-city").val();
    	getArea("#pre-district", pid, 3);
    });
});
/*
<div class="re-fra-info" id="${item.id}">
<div class="add-top-f height-0">
<p>姓名：<span>${item.contactPerson }</span></p>
<p>电话：<span>${item.contactTel }</span></p>
<p>地址：<span>${item.addressArea } ${item.contactAddress }</span></p>
</div>
<div class="add-bottom-f f-0 height-0">
<a href="javascript:setDefault('${item.id}')" class="setdefault"> 设为默认</a>
<a href="javascript:goUrl('<c:url value='/user/address?address_id=${item.id}&op=modify' />')" class="revise">修改</a>
<a href="javascript:delAdress('${item.id}')" class="delete">删除</a>
</div>
</div>
*/
function address_list_success(data){
var addressHtml ="";
$.each(JSON.parse(data), function(index, item) {
	addressHtml +="<div class=\"re-fra-info\" id=\""+item.id+"\">";
	addressHtml +="<div class=\"add-top-f height-0 height-auto " + (item.is_default== 1 ?"mr-b":"") + "\" id=\"sd" +item.id+"\">";
	addressHtml +="<p>客户姓名：<span>"+item.truename+"</span></p>";
	addressHtml +="<p>手机号码：<span>"+item.tel+"</span></p>";
	addressHtml +="<p class=\"height-auto\">详细地址：<span>"+item.address +"</span></p>";
	addressHtml +="</div>";
	addressHtml += "<div class=\"add-bottom-f f-0 height-0\">";
	addressHtml += "<a href=\"javascript:setDefault('"+item.id+"')\" class=\"setdefault\"> 默认</a>";
	addressHtml += "<a href=\"javascript:modifyAddress('"+item.id+"')\" class=\"revise\">修改</a>";
	addressHtml += "<a href=\"javascript:delAdress('"+item.id+"')\" class=\"delete\">删除</a>";
	addressHtml += "</div>";
	addressHtml += "</div>";
});
$("#addresses").empty();
$("#addresses").append(addressHtml);
}
function modifyAddress(id){
	goUrl($("#edit_url").val() + id);
}
function goAddUrl(url){
	goUrl(url);
}
function setDefault(id) {
	$.post($('#edit_url').val(), {'id': id, 'is_default': 1}, function(data){
		if(data){
			address_list_success(data);
			show_error('默认设置成功');
		}else{
			show_error('默认设置失败');
		}
	});
	// ajax_post_json($('#edit_url').val(), {
	// "id": id,
	// "is_default": 1
	// }, m_beforeSend, address_default_success, m_error, m_complete);
}
function address_default_success(data) {
	
}
function delAddress(id){
	confirm("亲，确认删除该地址吗？", "确认", "取消", function(s) {
		if (s == true) {
			$.post($('#delete_url').val(), {'id': id}, function(data){
			if(data){
				address_list_success(data);
				show_error('删除成功');
			}else{
				show_error('删除失败');
			}
	});
		}
	}, id);
}

function getArea(id, pid, type){
	var data = {"pid": pid, "type": type};
	$.ajax({
        type:"post",
        url: $("#get_address_url").val(),
        data: data,
        success: function Response(data){
        	var obj = eval('('+data+')');
        	var adHtml = "";
        	for(var i=0;i<obj.length;i++){
        		adHtml += "<option value='" + obj[i].id + "'>" + obj[i].name + "</option>";
        	}
        	$(id).empty();
        	$(id).append(adHtml);
        	if(type == 2){
        		getArea('#pre-district', $("#pre-city").val(), 3);
        	}
        	if(type == 3){
        		setSelectedAddress();
        	}
        }
    });
}
